import { toHump } from '@/utils/tools'
import { App } from 'vue'
import dataForm from './dataForm'

function install(app: App) {
  registerComponents(app)
}

function getComponentName(key: string) {
  if (!key) {
    return ''
  }
  const paths = key.split('/')
  const name = paths
    .filter((it) => !!it && it !== '.')
    .reverse()
    .find((it) => it !== 'index.vue' && it !== 'index.ts' && it !== 'index.js')
    ?.replace('.vue', '')

  return name || ''
}

function registerComponents(app: App) {
  const components = import.meta.globEager('./**.vue')
  Object.keys(components).forEach((key: string) => {
    const component = components[key]
    app.component(component.default.name || toHump(getComponentName(key)), component.default)
  })
  app.component('DataForm', dataForm)
}

export default {
  install,
}
